var state = false;
var page = 2;
var curr_cat = 0;
function bottomInfinite(){
    if(page){
        $("#products-list-loading img").show(function(){
            $.get(base_url+'products/next_page/'+curr_cat+'/'+page, function(data) {
                $('#products-list-loading').before(data);
                if(data.length > 10){
                    page++;
                }else{
                    page = false;
                }
                state=false;
                $("#products-list-loading img").hide();
            });
        });
    }else{
        $("#products-list-loading img").hide();
    }
}
$(document).ready(function(){
    // ADD PRODUCT TO CART
    $("#product-overlay").click(function(){
        /*$("#product-notification").hide();
        $("#product-overlay").hide();
        $("#product-cart-loading").css('visibility','hidden');
        */
    });
    $("#product-notif-close").click(function(){
        $("#product-notification").hide();
        $("#product-overlay").hide();
        location.reload();
    });
    $("#img-add-to-cart").click(function(){
        qty = parseInt($("#product-qty-input").val(),10);
        id = $("#product-id-input").val();
        if(!isNaN(qty)){
            $("#product-cart-loading").css('visibility','visible');
            $("#product-overlay").show();  
            $.ajax({
                type: "POST",
                url: base_url+"cart/add/"+id+"/"+qty
            }).done(function( msg ) {
                $("#product-cart-loading").css('visibility','hidden');
                var title = $("#product-name").html();
                $("#product-notif-title").html(title);
                $("#product-notification").show();
                $("#product-overlay").show();  
            });
        }else{
            alert('Please input correct number in product quantity');
        }
    });
    
    //INFINITE SCROLL
    $(window).scroll(function () { 
        if ($(window).scrollTop() >= $(document).height() - $(window).height()-10) {
            if(!state){
                state=true;
                bottomInfinite();
            }
        }
    });
});